<?php
class Email_management extends MY_Controller{
	
 	function __construct()
    {
    	parent::__construct();
    }
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
	function index(){
		$this->_setLayout('admin/email_management','admin_layout');
	}
	function add_email(){
    	$callback = $_REQUEST['callback'];
    	$jsonOutPut = '';
		try{
	    	$data = json_decode ($_GET['data'],true);
	    	if(empty($data)){
	    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));	    			    		
	    	}
	    	else{
		    	$this->load->model('Email');
		    	if(isset($data['id'])){
		    		$this->Email->update_email($data,$data['id']);
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$this->Email->get_by_id($data['id'])));
		    	}	
		    	else{	
		    		$id = $this->Email->insert_email($data);
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$this->Email->get_by_id($id)));
		    	}		 
	    	}
	    	   	
    	}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not load'));
    	}
    	if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}	
    }
	function delete_email(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
    		$this->load->model('Email');
    		$this->Email->delete_email($data);
			$jsonOutPut = json_encode(array('success'=>true));
		}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false));
    	}
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
	function email_grid(){
    	$this->load->model('Email');
	    $callback = $_REQUEST['callback'];
		// Create the output object.
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Email->get_total(),'data'=>$this->Email->get_email($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'ASC')));
		//start output
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
}